#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <unistd.h>

#define LOG_PATH  "/mnt/nandflash/mem.txt"

static void WriteLog(char *buf)
{
	FILE *fp = fopen(LOG_PATH, "a+");
	char log[1024] = {0};
	
	if(!fp){
		return ;
	}
	
	time_t t = time(NULL);
	struct tm *lt = localtime(&t);
	
	sprintf(log, "%02d-%02d:%02d:%02d@%s\n",lt->tm_mday, lt->tm_hour, lt->tm_min,
									lt->tm_sec, buf);
	printf("%s",log);
	fwrite(log, strlen(log), sizeof(char), fp);
	fclose(fp);
	
	return ;
}

/**--------------------------------------------------------------------------------
* 函数名称 :	MemInfo
* 功能描述 :	获取CPU内存使用大小及单个进程的内存使用大小
* 入口参数 :	char *app	进程名
* 编辑作者 :	hkm
* 返回值   ：   返回0：成功  -1：失败
* 备  注   ：   
---------------------------------------------------------------------------------*/
int MemInfo(char *app)
{
	const char *Path = "/proc/meminfo";
	int ret = 0;
	char buf[512] = {0};
	FILE *fp = NULL;
	unsigned int free_mem = 0;
	unsigned int total_mem = 0;
	unsigned int used_mem = 0;
	int app_mem = 0;
	char log[512] = {0};
	
	#if 1
	fp = fopen(Path, "r");
	if(fp == NULL){
		perror("fopen");
		return -1;
	}
	
	while(!feof(fp)){
		memset(buf, 0, sizeof(buf));
		fgets(buf, sizeof(buf), fp);
		if(strstr(buf, "MemTotal")){
			total_mem = atoi(buf + 10);
		}else if(strstr(buf, "MemFree")){
			free_mem = atoi(buf + 10);
		}
	}
	
	fclose(fp);
	#endif
	
	app_mem = AppMem(app);
	used_mem = total_mem - free_mem;
	sprintf(log, "total   %d ,used   %d ,free  %d ,%s mem  %d", 
						total_mem, used_mem, free_mem, app, app_mem);
	WriteLog(log);
	return 0;	
}

/**--------------------------------------------------------------------------------
* 函数名称 :	AppMem
* 功能描述 :	获取某个进程的内存使用大小
* 入口参数 :	char * progress  进程名
* 编辑作者 :	hkm
* 返回值   ：   成功返回进程的内存使用大小，失败返回0 
* 备  注   ：   
---------------------------------------------------------------------------------*/
int AppMem(char * progress)
{
	char buf[512] = {0};
	int pid = 0;
	char pid_buf[6] = {0};
	char cmd[512] = {0};
	FILE *fp = NULL;
	
	sprintf(cmd, "ps");
	fp = popen(cmd, "r");
	if(fp == NULL){
		printf("popen failed \n");
		return -1;
	}
	while(!feof(fp)){
		memset(buf, 0, sizeof(buf));
		fgets(buf, sizeof(buf), fp);
		if(strstr(buf, progress)){
			break;
		}
	}
	
	
	if(feof(fp)){
		sprintf(buf, "no such %s progress \n",progress);
		WriteLog(buf);
		pclose(fp);
		return 0;
	}
	pclose(fp);
	fp = NULL;
	pid = atoi(buf);
	
	memset(cmd, 0, sizeof(cmd));
	sprintf(cmd, "cat /proc/%d/status | grep VmRSS | awk '{print $2}'", pid);
	fp = popen(cmd, "r");
	if(!fp){
		printf("open '%s' failed \n",cmd);
		return -1;
	}
	
	memset(buf, 0, sizeof(buf));
	if(feof(fp)){
		sprintf(buf, "no such %d pid progress \n",pid);
		WriteLog(buf);
		pclose(fp);
		return 0;
	}
	fgets(buf, sizeof(buf), fp);
	pclose(fp);
	fp = NULL;
	return atoi(buf);
	
}


int main(int argc, char **argv)
{
	if(!access(LOG_PATH, F_OK)){
		remove(LOG_PATH);
	}
	
	int app_mem = 0;
	char buf[512] = {0};
	
	//if(argc < 2){
	//	printf("param failed \n");
	//	printf("notice : %s progress \n", argv[0]);
	//	return 0;
	//}
	
	while(1){
		MemInfo("TZ_GW_lora");
		//app_mem = AppMem("sftp");
		//memset(buf, 0, sizeof(buf));
		//sprintf(buf, "sftp mem : %d", app_mem);
		//WriteLog(buf);
		sleep(60);
	}
}